﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.Space.CoreTypes
{
    /// <summary>
    /// This class represents a table.
    /// </summary>
    [Serializable]
    public class SpaceTable : ISpaceEntry
    {
        /// <summary>
        /// The specified table data.
        /// </summary>
        [SpaceAttribute] 
        private readonly string[,] data;

        /// <summary>
        /// Initializes a new instance of the <see cref="SpaceTable"/> class.
        /// </summary>
        /// <param name="rows">The number of rows of the table.</param>
        /// <param name="columns">The number of columns of the table.</param>
        public SpaceTable(int rows, int columns)
        {
            this.data = new string[rows,columns];
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SpaceTable"/> class.
        /// </summary>
        /// <param name="data">The specified table data.</param>
        public SpaceTable(string[,] data)
        {
            this.data = (string[,]) data.Clone();
        }

        /// <summary>
        /// Gets the number of rows of the table.
        /// </summary>
        /// <value>The number of rows of the table.</value>
        public int Rows
        {
            get { return this.data.Length; }
        }

        /// <summary>
        /// Gets the number of columns of the table.
        /// </summary>
        /// <value>The number of columns of the table.</value>
        public int Columns
        {
            get { return this.data.GetLength(1); }
        }

        /// <summary>
        /// Gets or sets the <see cref="System.String"/> at the specified row and column.
        /// </summary>
        /// <value>The specified value at the specified row and column.</value>
        public string this[int row, int column]
        {
            get { return this.data[row, column]; }
            set { this.data[row, column] = value; }
        }

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public object Clone()
        {
            return CloneFunctions.Clone(this);
        }

        /// <summary>
        /// Compares the current <see cref="ISpaceEntry"/> to the specified <see cref="ISpaceEntry"/>.
        /// </summary>
        /// <param name="entry">The specified entry to compare.</param>
        /// <returns>
        /// If the object instances are even, the method returns true otherwise, false.
        /// </returns>
        public bool Equals(ISpaceEntry entry)
        {
            if (entry is SpaceTable)
            {
                SpaceTable check = (SpaceTable)entry;

                if ((this.Rows == check.Rows) && (this.Columns == check.Columns))
                {
                    for (int i = 0; i < check.Rows; i++)
                    {
                        for (int j = 0; j < check.Columns; j++)
                        {
                            if (this[i, j].CompareTo(check[i, j])!=0)
                            {
                                return false;
                            }
                        }
                    }

                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Compares the current <see cref="ISpaceEntry"/> to the specified <see cref="ISpaceEntry"/>.
        /// </summary>
        /// <param name="entry">The specified entry to compare.</param>
        /// <returns>
        /// If the specified object instance is lower than the current instance the method returns true otherwise,
        /// false.
        /// </returns>
        public bool LowerThan(ISpaceEntry entry)
        {
            return false;
        }

        /// <summary>
        /// Compares the current <see cref="ISpaceEntry"/> to the specified <see cref="ISpaceEntry"/>.
        /// </summary>
        /// <param name="entry">The specified entry to compare.</param>
        /// <returns>
        /// If the specified object instance is greater than the current instance the method returns true otherwise,
        /// false.
        /// </returns>
        public bool GreaterThan(ISpaceEntry entry)
        {
            return false;
        }
    }
}